<template>
  <div :id="id" :class="className" style="width: 100%; height: 100%" />
</template>

<script>
import echarts from 'echarts'
import resize from './mixins/resize'
import { array } from 'jszip/lib/support'
import dayjs from 'dayjs'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart',
    },
    id: {
      type: String,
      default: 'chart',
    },
    width: {
      type: String,
      default: '200px',
    },
    height: {
      type: String,
      default: '200px',
    },
    list: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    list: {
      handler(newValue, oldValue) {
        this.initChart()
      },
      deep: true,
    },
  },
  // mounted() {
  //   if (this.list.length > 0) {
  //     this.initChart()
  //   } else {
  //     console.log(55)
  //   }
  // },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      const xData = [],
        yData = [],
        sData = []
      for (let i = 1; i < this.list.length; i++) {
        // xData.push(dayjs(this.list[i].start_time).format('MM-DD HH:mm:ss'))
        xData.push(this.list[i].start_time1)
        yData.push(this.list[i].broken)
        sData.push(this.list[i].speed)
      }
      // const xData = (function () {
      //   const data = []
      //   for (let i = 1; i < this.list.length; i++) {
      //     data.push(this.list[i].start_time1)
      //   }
      //   return data
      // })()
      // const yData = (function () {
      //   const data = []
      //   for (let i = 1; i < this.list.length; i++) {
      //     data.push(this.list[i].broken)
      //   }
      //   return data
      // })()
      // console.log(xData, yData)
      this.chart.setOption({
        backgroundColor: '#fff',
        // title: {
        //   text: 'statistics',
        //   x: '20',
        //   top: '20',
        //   textStyle: {
        //     color: '#fff',
        //     fontSize: '22',
        //   },
        //   subtextStyle: {
        //     color: '#90979c',
        //     fontSize: '16',
        //   },
        // },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            textStyle: {
              color: '#fff',
            },
          },
        },
        grid: {
          left: '5%',
          right: '5%',
          borderWidth: 0,
          top: 150,
          bottom: 95,
          textStyle: {
            color: '#fff',
          },
        },
        legend: {
          x: '5%',
          top: '10%',
          textStyle: {
            color: '#90979c',
          },
          data: ['新增断头', '速度'],
          // data: ['female', 'male', 'average'],
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            axisLine: {
              lineStyle: {
                color: '#90979c',
              },
            },
            splitLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitArea: {
              show: false,
            },
            // axisLabel: {
            //   interval: 0,
            // },
            data: xData,
          },
        ],
        yAxis: [
          {
            type: 'value',
            splitLine: {
              show: false,
            },
            axisLine: {
              lineStyle: {
                color: '#90979c',
              },
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              interval: 0,
            },
            splitArea: {
              show: false,
            },
          },
        ],
        dataZoom: [
          {
            show: true,
            height: 30,
            xAxisIndex: [0],
            bottom: 30,
            start: 10,
            end: 80,
            handleIcon: 'path://M306.1,413c0,2.2-1.8,4-4,4h-59.8c-2.2,0-4-1.8-4-4V200.8c0-2.2,1.8-4,4-4h59.8c2.2,0,4,1.8,4,4V413z',
            handleSize: '110%',
            handleStyle: {
              color: '#d3dee5',
            },
            textStyle: {
              color: '#fff',
            },
            borderColor: '#90979c',
          },
          {
            type: 'inside',
            show: true,
            height: 15,
            start: 1,
            end: 35,
          },
        ],
        series: [
          {
            name: '断头数',
            type: 'bar',
            stack: 'total',
            barMaxWidth: 10,
            barGap: '10%',
            itemStyle: {
              normal: {
                color: 'rgba(255,144,128,1)',
                label: {
                  show: false,
                  textStyle: {
                    color: '#fff',
                  },
                  position: 'Top',
                  formatter(p) {
                    return p.value > 0 ? p.value : ''
                  },
                },
              },
            },
            data: yData,
          },

          // {
          //   name: 'male',
          //   type: 'bar',
          //   stack: 'total',
          //   itemStyle: {
          //     normal: {
          //       color: 'rgba(0,191,183,1)',
          //       barBorderRadius: 0,
          //       label: {
          //         show: true,
          //         position: 'top',
          //         formatter(p) {
          //           return p.value > 0 ? p.value : ''
          //         },
          //       },
          //     },
          //   },
          //   data: [327, 1776, 507, 1200, 800, 482, 204, 1390, 1001, 951, 381, 220],
          // },
          {
            name: '速度',
            type: 'line',
            // stack: 'total',
            symbolSize: 10,
            symbol: 'circle',
            itemStyle: {
              normal: {
                color: 'rgba(5,105,225,1)',
                barBorderRadius: 0,
                label: {
                  show: true,
                  position: 'top',
                  formatter(p) {
                    return p.value > 0 ? p.value : ''
                  },
                },
              },
            },
            data: sData,
          },
        ],
      })
    },
  },
}
</script>
